package com.example.uCenter.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.uCenter.common.Code;
import com.example.uCenter.common.Result;
import com.example.uCenter.user.entity.User;
import com.example.uCenter.user.service.IUserService;
import io.swagger.models.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.rowset.serial.SerialBlob;
import java.sql.Blob;
import java.util.List;
import java.util.Objects;

/**
 * @author xxs
 * @since 2022-08-27 08:20:38
 */
@CrossOrigin
@RestController
@RequestMapping("/UCenter")
public class UserController {

    @Autowired
    IUserService IUserService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/user")
    public Result Login(@RequestBody User user) {
        return IUserService.Login(user.getUserName(), user.getPassword());
    }

    /**
     * 分页查询
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/userPaging")
    public Result UserPaging(@RequestParam Integer current, @RequestParam Integer size) {
        return IUserService.findPaging(current, size);
    }

    /**
     * 添加数据
     *
     * @param user
     * @return
     */
    @PostMapping("/add")
    public Result AddUser(@RequestBody User user) {
        return IUserService.AddUser(user);
    }

    /**
     * 根据用户ID删除
     * @param userId
     * @return
     */
    @DeleteMapping("/remove/{userId}")
    public Result RemoveUser(@PathVariable Integer userId) {
        return IUserService.RemoveUser(userId);
    }

    /**
     * 批量删除
     * @param userIds
     * @return
     */
    @DeleteMapping("/removeList")
    public Result RemoveUserAll(@RequestBody List<Integer> userIds) {
        return IUserService.RemoveUserAll(userIds);
    }

    /**
     * 修改信息
     *
     * @param user
     * @return
     */
    @PutMapping("/edit")
    public Result EditUser(@RequestBody User user) {
        Result result = new Result(false, Code.UPDATE_ERROR, "数据修改失败！！！");
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_id", user.getUserId());
        if (IUserService.update(user, qw)) {
            result.setData(true);
            result.setCode(Code.UPDATE_OK);
            result.setMsg("数据修改成功！！！");
        }
        return result;
    }

    /**
     * 通过用户id上传图片
     *
     * @param file   图片
     * @param userId 用户id
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public Result Upload(@RequestParam MultipartFile file, @RequestParam Integer userId) {
        Result result = new Result(false, Code.UPDATE_ERROR, "图片上传失败！！！");
        try {
            if (!file.isEmpty()) {
                User user = IUserService.getById(userId);
                Blob blob = new SerialBlob(file.getBytes());
                user.setAvatar(blob);
                QueryWrapper qw = new QueryWrapper();
                qw.eq("user_id", userId);
                boolean b = IUserService.update(user, qw);
                if (b) {
                    result.setData(true);
                    result.setCode(Code.UPDATE_OK);
                    result.setMsg("图片上传成功！！！");
                }
            }
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * 前台获取头像
     *
     * @param userId 用户id
     * @return
     */
    @GetMapping("/getImage")
    public Result getImage(@RequestParam String userId) {
        Result result = new Result(null, Code.GET_ERROR, "数据查询失败！！！");
        User user = IUserService.getById(userId);

        System.out.println("的广泛工程费功夫阿顺丰噶尔个个人和" + user);

        if (Objects.nonNull(user.getAvatar())) {
            result.setData(user.getAvatar());
            result.setCode(Code.GET_OK);
            result.setMsg("数据查询成功！！！");
        }
        return result;
    }
}

